import { getUserPermissionMenus } from '@/api/permission'
import { constantRoutes, filterRoutes, addRoutes } from '@/router'

const state = {
  permissionMenus: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.permissionMenus = constantRoutes.concat(routes)
    filterRoutes(routes);
    addRoutes(routes);
  }
}

const actions = {
  generateRoutes({ commit }) {
    return new Promise((resolve, reject) => {
      getUserPermissionMenus().then(data => {
        commit('SET_ROUTES', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })

  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}